<?php

/**
 * Flickr Photosets - Map Frame Page
 * Displays the Google map for the photo or photoset.
 * 
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 */

/*
Require the function library
This will include all other necessary files
*/
require_once 'lib.php';

// Get the ids
$uid = $_GET['uid'];
$pid = $_GET['pid'];
$sid = $_GET['sid'];

// Set up Flickr
$flickr = new phpFlickr(FLICKR_API_KEY, FLICKR_API_SECRET);

if (!empty($pid)) {
	$type = 'photo';
	$geo = $flickr->photos_geo_getLocation($pid);
	$photo = $flickr->photos_getInfo($pid);
	
	// If there is no geodata, stop
	if (empty($geo)) {
		echo 'Sorry, geodata is not available for this photo.';
		die();
	}

	$formatted_description = str_replace("\n", '', nl2br($photo['description']));
	$formatted_description = str_replace("\r", '', $formatted_description);
}
else if (!empty($sid)) {
	$type = 'set';
	$photoset = $flickr->photosets_getPhotos($sid);
	
	// Create an array with photo info on all geotagged photos
	$photos = array();
	foreach ($photoset['photo'] as $photoset_photo) {
		$photo = $flickr->photos_getInfo($photoset_photo['id']);
		if (isset($photo['location'])) {
			$info = array(
				'id' => $photo['id'],
				'secret' => $photo['secret'],
				'server' => $photo['server'],
				'farm' => $photo['farm'],
				'title' => $photo['title'],
				'description' => $photo['description'],
				'secret' => $photo['secret'],
				'location' => array(
					'latitude' => $photo['location']['latitude'],
					'longitude' => $photo['location']['longitude'],
				)
			);
			$photos[] = $info;
		}
	}
	
	// If there are no photos with geodata, stop
	if (count($photos) == 0) {
		echo 'Sorry, no photos in this set have geodata.';
		die();
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>Map of <?=$photo['title']?></title>
	<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=<?=GOOGLEMAPS_API_KEY?>" type="text/javascript"></script>
    <script type="text/javascript">
    
	function createMarker(point, infoContent) {
		var marker = new GMarker(point);
		GEvent.addListener(marker, 'click', function() {
			marker.openInfoWindowHtml(infoContent);
		});
		return marker;
	}
	
	function buildPhotoUrl(photo, size) {
		return 'http://farm' + photo['farm'] + '.static.flickr.com/' + photo['server'] + '/' + photo['id'] + '_' + photo['secret'] + '_' + size + '.jpg';
	}
	
	function initialize(type) {
		if (GBrowserIsCompatible()) {
			
			// Create the map and add controls
			var map = new GMap2(document.getElementById("map_canvas"));
			map.addMapType(G_PHYSICAL_MAP);
			map.addControl(new GSmallMapControl());
			map.addControl(new GHierarchicalMapTypeControl());
			
			<?php if ($type == 'photo') { ?>
				
				// Add the point for the photo
				var thePoint = new GLatLng(<?=$geo['location']['latitude']?>, <?=$geo['location']['longitude']?>);
				map.setCenter(thePoint, 15);
				
				// Preload the image
				var theImage = new Image();
				theImage.src = "<?=$flickr->buildPhotoUrl($photo, 'small')?>";
				theImage.onload = function() {
					theMarker.openInfoWindowHtml(theMarkerContent);
				};
				
				// Marker info
				var theTitle = "<?=$photo['title']?>";
				var theDescription = "<?=$formatted_description?>";
				var theMarkerContent = '<div id="info"><img src="' + theImage.src + '" /></div>';
				
				// Add the marker
				var theMarker = createMarker(thePoint, theMarkerContent);
				map.addOverlay(theMarker);
				
			<?php } else if ($type == 'set') { ?>
				
				// We have to center before adding markers
				map.setCenter(new GLatLng(0, 0), 0);
				var bounds = new GLatLngBounds();
				
				var photos = <?=json_encode($photos)?>;
				var images = []
				var markers = [];
				
				// Add the markers
				for (i = 0; i < photos.length; i++) {
					
					// Preload the image
					images[i] = new Image();
					images[i].src = buildPhotoUrl(photos[i], 'm');
					
					// Create the point
					var thePoint = new GLatLng(photos[i]['location']['latitude'], photos[i]['location']['longitude']);
					var theText = '<div id="info" style="max-width: 260px;"><img src="' + images[i].src + '" /><h2>' + photos[i]['title'] + '</h2><p>' + photos[i]['description'] + '</p></div>';
					markers[i] = createMarker(thePoint, theText);
					
					// Add to the map and extend the bounds
					map.addOverlay(markers[i]);
					bounds.extend(thePoint);
					
				}
				
				// Determine center and bounds
				map.setZoom(map.getBoundsZoomLevel(bounds));
				map.setCenter(bounds.getCenter());
				
			<?php } ?>
			
			// Set the map type to terrain
			var mapTypes = map.getMapTypes();
			for (i = 0; i < mapTypes.length; i++) {
				if (mapTypes[i].getName() == 'Terrain') {
					map.setMapType(mapTypes[i]);
					break;
				}
			}
			
		}
	}

    </script>
	
	<style>
		body { margin: 0; padding: 0; font-family: 'lucida grande', tahoma, verdana, arial, sans-serif; font-size: 11px; }
		#info { text-align: center; }
			#info h2 { font-size: 12px; font-weight: bold; }
	</style>
	
</head>

<body onload="initialize();" onunload="GUnload()">
	
	<div id="map_canvas" style="height:<?=$_GET['h']?>px;"></div>
	
</body>
</html>
